package top.ffhome.mail;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import java.io.File;
import java.util.List;

public class MailApp {
    private static final int MAX_COUNT = 8; // 含最大值：0-MAX_COUNT

    public static void main(String[] args) {
        Log log = LogFactory.get();
        String subjectPrefix = "【默认】 ";
        if (args.length > 0) {
            subjectPrefix = "【" + args[0] + "】 ";
        }
        log.info("邮件主题前缀:{}", subjectPrefix);
        String path = MailApp.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        List<String> list = StrUtil.split(path, "/");
        list.set(list.size() - 1, "temp");
        path = StrUtil.join("/", list);
        log.info("发送邮件的目录:{}", path);
        File[] files = FileUtil.ls(path);
        int i = RandomUtil.randomInt(0, MAX_COUNT);
        for (File file : files) {
            String addr = "backup" + i + "@ffhome.top";
            log.info("收件邮箱:{},当前文件:{}", addr, file.getPath());
            String result = MailUtil.send(addr, subjectPrefix + file.getName(),
                    file.getName(), false, file);
            log.info("发送结果:{}", result);
            i++;
            if (i > MAX_COUNT) i = 0;
        }
    }
}
